package com.xszx.server.service;

import com.xszx.server.dto.Page;
import com.xszx.server.entity.Category;
import com.xszx.server.entity.Chapter;
import com.xszx.server.entity.Course;
import com.xszx.server.entity.CourseCategory;
import com.xszx.server.mapper.ChapterMapper;
import com.xszx.server.mapper.CourseCategoryMapper;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CourseCategoryService {

    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    //批量添加课程分类
    @Transactional
    public void addBatch(Course course){
        //删除课程对应分类
        courseCategoryMapper.deleteByCourseId(course.getId());
        //添加分类
        for(Category category : course.getCategorys()){
            courseCategoryMapper.insert(new CourseCategory(UuidUtil.getShortUuid(),course.getId(),category.getId()));
        }
    }

    //根据课程的ID获取分类
    public List<Category> getCategorysByCourseId(String courseId){
        return courseCategoryMapper.findCategorysByCourseId(courseId);
    }
}
